package com.jnu.erp.framework.web.domain;

import lombok.Data;

import java.io.Serializable;
import com.jnu.erp.framework.web.exception.IResultCode;

import static com.jnu.erp.framework.web.domain.ApiResultCode.FAILED;
import static com.jnu.erp.framework.web.domain.ApiResultCode.SUCCESS;

/**
 * 响应信息主体
 * @param <T>
 */
@Data
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;
    private String message;
    private T body;

    private ApiResult(int code, String message, T body) {
        this.code = code;
        this.message = message;
        this.body = body;
    }

    /**
     * 成功返回结果
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ApiResult success(T data){
        return new ApiResult<>(SUCCESS.getCode(), "操作成功", data);
    }

    /**
     * 成功返回结果
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResult success(){
        return success(null);
    }

    /**
     * 失败返回结果
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ApiResult failed(String message){
        return new ApiResult<>(FAILED.getCode(), message, null);
    }

    public static <T> ApiResult failed(IResultCode iResultCode){
        return new ApiResult<>(iResultCode.getCode(), iResultCode.getMessage(), null);
    }

    public static <T> ApiResult invalidParameter(String message){
        return new ApiResult<>(FAILED.getCode(), message, null);
    }
}
